package DAO;

import Connect.ConnectDB;
import java.awt.List;
import javax.swing.*;
import java.sql.*;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

public class DepDAO {

    Connection conn;
    ResultSet rsDep = null;
    Statement st;
    PreparedStatement pst;

    public DepDAO() {
        ConnectDB cdb = new ConnectDB();
        cdb.getConnect();
        this.conn = cdb.conn;

        loadDataDep();
    }

    public void loadDataDep() {
        try {
            String query = "SELECT "
                    + "             * "
                    + "     FROM "
                    + "             Departments";
            st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rsDep = st.executeQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public DefaultTableModel loadTableDep() {
        try {
            String query = "Select * From Departments";
            Statement ps = conn.createStatement();
            rsDep = ps.executeQuery(query);

            Vector nameCol = new Vector();
            nameCol.add("Department ID");
            nameCol.add("Department Name");
            nameCol.add("Department Head");
            nameCol.add("Location");


            DefaultTableModel dtm = new DefaultTableModel(nameCol, 0);

            while (rsDep.next()) {

                Vector dataRow = new Vector();
                dataRow.add(rsDep.getString("DEPTNO"));
                dataRow.add(rsDep.getString("DEPTNAME"));
                dataRow.add(rsDep.getString("DEPTHEAD"));
                dataRow.add(rsDep.getString("LOCATION"));
                dtm.addRow(dataRow);
            }
            return dtm;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
